package com.svo.weitu.activity.fragment;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.svo.platform.utils.HttpUtil;
import com.svo.platform.utils.SvoToast;
import com.svo.platform.utils.SvoUtil;
import com.svo.platform.widget.LoadDialog;
import com.svo.platform.widget.PushRefreshListView;
import com.svo.platform.widget.PushRefreshListView.OnRefreshListener;
import com.svo.weitu.OrderAdapter;
import com.svo.weitu.R;
import com.svo.weitu.activity.MainActivity;
import com.svo.weitu.model.db.ResData;
import com.svo.weitu.model.entity.MenuEntity;
import com.svo.weitu.model.push.Utils;
import com.svo.weitu.util.Cons;
import com.svo.weitu.util.MyUtil;

public class OrderFragment extends Fragment implements OnItemClickListener{
	private PushRefreshListView listView;
	private List<MenuEntity> entities;
	private OrderAdapter orderAdapter;
	private View root;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.order, null);
		listView = (PushRefreshListView) root.findViewById(R.id.orderList);
		root.findViewById(R.id.menu).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).showMenu();
			}
		});
		listView.setOnItemClickListener(this);
		listView.setonRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				netReq();
			}
		});
		refresh();
		if (entities == null || entities.size() == 0) {
			netReq();
		}
		return root;
	}
	private LoadDialog loadDialog;
	private void netReq() {
		if (MyUtil.isNetworkAvailable(getActivity())) {
			String qudao = Utils.getMetaValue(getActivity(), "UMENG_CHANNEL");
			int version = SvoUtil.getVersion(getActivity());
			HttpUtil.get(Cons.gen+"res/get?qudao="+qudao+"&version="+version, null, new AsyncHttpResponseHandler(){
				@Override
				public void onStart() {
					super.onStart();
					if (loadDialog == null) {
						loadDialog = new LoadDialog(getActivity());
					}
					loadDialog.show();
				}
				@Override
				public void onSuccess(String arg0) {
					super.onSuccess(arg0);
					if (loadDialog != null) {
						loadDialog.dismiss();
					}
					listView.onRefreshComplete();
					List<MenuEntity> lists = new Gson().fromJson(arg0, new TypeToken<List<MenuEntity>>(){}.getType());
					ResData resData = new ResData(getActivity());
					resData.clear();
					resData.insert(lists);
					refresh();
				}
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					super.onFailure(arg0, arg1);
					listView.onRefreshComplete();
					if (loadDialog != null) {
						loadDialog.dismiss();
					}
				}
			});
		}else {
			SvoToast.showHint(getActivity(), "网络不通", Toast.LENGTH_SHORT);
		}
	}
	private void refresh() {
		try {
			entities = new ResData(getActivity()).queryAll();
			Vector<Integer> ids = new ResData(getActivity()).getSubIds();
			orderAdapter = new OrderAdapter(getActivity(), entities,ids);
			listView.setAdapter(orderAdapter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		listView.setItemChecked(position, true);
	}
}
